(function ($) {
    let ptname = 'suncsf-upload-context';
    let endid = 'suncsf-upload-end';
    let accept = 'image/*';
    let name = '';
    $.extend({
        initSuncUploadImg: function (_name, _endid,m) {
            if (_endid) {
                endid = _endid;
            }
            if(m){
                accept = m;
            }
            if(_name){
                name = _name;
            }
            $.createuploadimg();
        },
        createuploadimg: function () {
            //--------------------
            //创建单个加载上传图片容器
            let content = document.createElement('div');
            content.className = 'suncsf-upload-image add';
            //调用 input file的click即选择事件
            content.onclick = function () {
                if ($(this).hasClass('add')) {
                    let fileinput = $(this).children('input[type=file]')[0];
                    fileinput.click();
                    return;
                }
                if (confirm('是否移除？')) {
                    $(this).remove();
                }
            };
            //--------------------
            //创建图片加载
            let uplaodimg = document.createElement('img');
            uplaodimg.src = './img/添加.png';
            //加入容器
            content.appendChild(uplaodimg);

            //--------------------
            //隐藏的input file 
            let fileinput = document.createElement('input');
            // fileinput.name = name+'temp';
            fileinput.type = 'file';
            fileinput.accept = accept;
            //设置change事件 回显图片
            fileinput.onchange = function () {
                // let _this = this;
                let files = this.files;
                var count = files.length;
                // alert(count);
                if (count > 0) {
                    let file = files[0];
                    var img = $(this).prev('img');
                    if (img) {
                        var fr = new FileReader();
                        console.log('111111111111111111',this.result);
                        fr.onload = function () {
                            img.attr('src', this.result);
                        }
                        fr.readAsDataURL(file);
                        $(this).parent().removeClass('add');
                        $(this).prop('name',name);
                        $.createuploadimg();
                    }
                }
            };
            //加入容器
            content.appendChild(fileinput);
            //--------------------加入到主容器内清除浮动div元素之前
            document.getElementsByClassName(ptname)[0].insertBefore(content, document.getElementById(endid));
            // $('#suncsf-upload-end').before('<div class="suncsf-upload-image" onclick="uploadimgclick(this);"><img src="./img/添加.png" /><input type="file" name="file" onchange="uploadimgload(this);" /></div>');
        }
    })
})(jQuery);